/*
	------------------------------------------------------------
	Convert an unordered list of links into a select dropdown list.

	<ul>
		<li>Select a State</li>
		<li><a href="#Alabama">Alabama</a></li>
		...
	</ul>

	Becomes:
	<div style="display: inline-block; position: relative; width: 270px; height: 43px; " class="select_dropdown_container">
		<span class="select_dropdown_value">Select State</span>
		<select class="select_dropdown_select" style="position: absolute; bottom: 0px; left: 0px; opacity: 0; display: block; ">
			<option disabled="disabled">Select State</option>
			<option value="%23Alabama">Alabama</option>
			...
		</select>
	</div>	
	------------------------------------------------------------
*/

(function($) {
$(function() {

$.fn.select_dropdown = function(parameters) {
	var settings = $.extend({
		prepend: 'select_dropdown_',
		onChange: function(e) {
			var option = e.srcElement.options[e.srcElement.selectedIndex];
			document.location = unescape(option.value);
		}
	}, parameters || {});

	return this.each(function() {
		var $ul = $('ul', this);
		if ( !$ul.length ) return;
		$ul.hide();
		var $select = $('<select/>');

		$select
			.addClass(settings.prepend + 'select');

		var selected_found = false;
		var value_text;
		$('li', $ul).each(function() {
			var $li = $(this);
			var $a = $('a', this);
			var $option = $('<option/>');
			if ( $a[0] ) {
				$option
					.attr({value: escape($a.attr('href'))})
					.html($a.text());
			} else {
				$option
					.attr({disabled: 'disabled'})
					.html($li.text());
			}

			// Look for class="selected" on any elements to use as default state, otherwise use first element
			if ( !selected_found && $li.is('.selected') ) {
				selected_found = true;
				value_text = $li.text();
				$option.attr({selected: 'selected'});
			}
			if ( !value_text ) value_text = $li.text();

			$select.append($option);
		});

		if ( settings.onChange ) {
			$select.bind('change', settings.onChange);
		}

		$ul.after($select);
	});
};

});
})(jQuery);